﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime;
using System.Diagnostics;
using YakShaver.Contract;
namespace YakShaver.YakServer
{
    public class WorkItemService: IWorkItemService
    {
        #region IWorkItemService Members

        private WorkflowRuntime _workflowRuntime = null;

        public WorkItemService()
        {
            // create the workflow runtime or find it... 
            throw new ArgumentException("Workflow Runtime is null... so, if your reading this that means your far enough along that you need to unit test the constructor.");

        }

        public WorkItemService(WorkflowRuntime runtime)
        {
            _workflowRuntime = runtime;
        }
        public Guid SubmitWorkItem(IWorkItemDataContract dataContract)
        {
            // this is called from the capture application.  So we will put this into the workflow.  Passing back
            // the guid as a reference ID.  hmm.. means changing the contract again.. shit.

            // the workflow will take over and put it into holding.  Then the workflow will do the analysis to the provider 
            // to see if it fits certain criteria and then if it goes into the workflow item tracker or whatever.  it may
            // decide to do that regardless.  

            //YakShaver.ReleaseManagerStateFlow stateWorkflowInstance = new ReleaseManagerStateFlow();
            WorkflowInstance workflowInstance = null;
            Dictionary<string, object> namedParameters = new Dictionary<string, object>();
            namedParameters.Add("WorkItemDataContract", dataContract);


            workflowInstance = _workflowRuntime.CreateWorkflow(typeof(WorkItemEntryWorkflow), namedParameters);
            workflowInstance.Start();
            //stateWorkflowInstance = _workflowRuntime.GetWorkflow(workflowInstance.InstanceId);

            //Trace.WriteLine(string.Format("Instance: {0} - State: {1}", workflowInstance.InstanceId, stateWorkflowInstance.CurrentStateName));
            return workflowInstance.InstanceId;
        }

        #endregion
    }
}
